<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>登录页</title>
    <style>
        html,
        body {
            height: 100%;
        }

        body {
            background-image: url(./img/login-background.jpg);
            display: flex;
            justify-content: center;
            align-items: center;
        }

        .box {
            min-width: 400px;
            background-color: white;
            border-radius: 10px;
            overflow: hidden;
        }

        .box h2 {
            text-align: center;
            line-height: 60px;
            color: gray;
        }

        .box .content {
            margin: 30px;
        }

        .box .content .one {
            display: flex;
            justify-content: space-between;
            margin-bottom: 20px;
        }

        .box .content .one input {
            width: 100%;
            box-sizing: border-box;
            height: 40px;
            border-radius: 5px;
            border: 1px solid #ccc;
            padding-left: 15px;
            outline: none;
        }

        .box .content .one a {
            text-decoration: none;
            color: #555;
        }

        .box .content .one.two {
            display: flex;
            justify-content: center;
        }

        .box .content .one button {
            border: none;
            background-color: lightgreen;
            color: white;
            padding: 10px 20px;
            border-radius: 5px;
            width: 100%;
        }
    </style>
</head>

<body>
    <div class="box">

        <h2>欢迎登陆后台管理系统</h2>
        <div class="content">
            <div class="one">
                <input type="text" placeholder="输入用户名">
            </div>
            <div class="one">
                <input type="password" placeholder="输入密码">
            </div>
            <div class="one two">
                <button>登陆</button>
            </div>
        </div>
    </div>
</body>

</html>
<script src="./axios.min.js"></script>
<script>
    //获取输入框
    var inp = document.querySelectorAll('input');
    //获取登陆按钮
    var loginBtn = document.querySelector('button');

    //给登录按钮 绑定点击事件
    loginBtn.onclick = () => {
        //发起登陆请求
        axios.post('/user/login', { phone: inp[0].value, pass: inp[1].value }).then((res) => {
            if (res.data.code == 200) {

                //将token存储到了localStorage中
                localStorage.setItem('token', res.data.token);

                //将服务器返回的用户信息, 存储到了localStorage中
                localStorage.setItem('userinfo', JSON.stringify(res.data.userinfo));
                alert('登陆成功!');
                location.href = './index.html';
            } else {
                console.log(res.data)
                alert('登陆失败!')

            }
        })
    }
</script>